import { Component, Injector} from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL } from "./../../providers/Config";
import { Events } from 'ionic-angular';


/**
 * Generated class for the FollowTeacherComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'follow-teacher',
  templateUrl: 'follow-teacher.html'
})
export class FollowTeacherComponent extends BasePage {

  picrooturl = "";//图片服务地址
  userinfo = {}//获取内存中的数据
  focuselist = [];//关注过的店铺
  flag1:boolean;
  is_focus=1; //关注显示器
 
  constructor(injector: Injector,public events: Events, public userService: UserService, public stc: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL
    this.userinfo = this.stc.read('userinfo');
    this.getUserFocusAPI();
   
  }

  /**
   * 选择要关注哪些美甲师
   * @param i   哪一个
   * @param j   关注还是取消关注   1是已关注  0是取消关注
   */
  chose(i,j){
    let id=[];
    i['focused']=j;
    console.log(this.focuselist)
    this.focuselist.forEach((e)=>{
      if(e.focused == 0){
        id.push(e.artist_id);
      }
    })
    console.log(id);
    this.stc.write("focuse1",id);
  }
  /**取消美甲师的关注 */
  // async focusAPI(item) {
  //   let param = { "focused_id": item['artist_id'], 'user_id': this.userinfo['user_id'] };
  //   let loader = this.loadingCtrl.create({
  //     content: "",
  //   });
  //   let result = await this.userService.focusAPI(param);
  //   loader.dismiss();
  //   if (result.code == 1) {
  //     this.getUserFocusAPI();
  //   } else {
  //     this.showAlert(result.msg);
  //   }
  // }


  /**获取美甲师的关注列表 */
  async getUserFocusAPI() {
    console.log(147)
    let param = { 'focus_type': '1', 'user_id': this.userinfo['user_id'] };
    let loader = this.loadingCtrl.create({
      content: "",
    });
    let result = await this.userService.getUserFocusAPI(param);
    console.log(result)
    loader.dismiss();
    if (result.code == 1) {
      if(result.data.length == 0){
        this.flag1=true
      }else{
        this.flag1=false;
        this.focuselist = result.data.list;
        this.focuselist.forEach((e)=>{
          e['focused']=1
        })
      }
    }else if( result.code == 1001){
      this.flag1=true
      console.log("没关注任何美甲师")
    }else{
      this.flag1=true;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }
            
/*跳转到美甲师*/
  home(){
    this.navCtrl.push('about');

  }
  //跳转到美甲师详情
  gotoNailDetail(item,e){
    console.log();
    if(e.target.className =='focusBtn'){
      return false;
    }else{
      let list=item;
      let modal = this.modalCtrl.create("NailerDetailPage", {list:list});
      modal.onDidDismiss(data => {

      });
      modal.present();
    }
  }
}
